<?php

namespace App\Admin\Controllers;

use App\Website;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Support\MessageBag;

class WebsiteController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('网站建设')
            ->description('网站建设')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('网站建设')
            ->description('网站建设---只可启用一个')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('网站编辑')
            ->description('网站编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('网站建设--新建')
            ->description('网站建设--新建')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Website);

        $grid->id('Id');
        $grid->title('网站标题');
        $grid->key('关键字');
        $grid->logo('首页封面图');
        $grid->telephone('客服电话');

        $grid->des('网站描述');
        $grid->column('yulan','预览')->display(function($vlaue){
            return "<a href='".config('admin.app_outurl')."/#/' target='_blank'> 查看</a>";
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Website::findOrFail($id));

        $show->id('Id');
        $show->title('网站标题');
        $show->key('关键字');
        $show->logo('首页封面图');
        $show->telephone('客服电话');

        $show->des('网站描述');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Website);

        $form->text('title', '网站标题')->rules('required',[
            'required'=>'请输入网站标题'
        ])->help('必填项');
        $form->text('key', '关键字')->rules('required',[
            'required'=>'请输入网站关键字'
        ])->help('必填项');
        $form->image('logo', '首页封面图')->help('必选项');
        $form->mobile('telephone', '客服电话')->options(['mask' => '999 9999 9999']);
        $form->image('watermark', '图片水印')->help('必选项');
        $type = [
            'SouthEast' => '右下',
            'NorthWest' => '左上',
            'North' => '上',
            'NorthEast' => '右上',
            'West' => '左',
            'Center' => '中心',
            'East' => '右',
            'SouthWest' => '左下',
            'South' => '下',
        ];
        $form->select('watermark_position', '水印位置')->options($type);
        $form->textarea('desc', '网站描述')->rules('required',[
            'required'=>'请输入网站描述'
        ])->help('必填项');
        $form->textarea('script','百度统计代码');
        $form->tools(function (Form\Tools $tools) {

            // 去掉`列表`按钮
            $tools->disableList();

            // 去掉`删除`按钮
            $tools->disableDelete();

            // 去掉`查看`按钮
            $tools->disableView();

        });
        $form->footer(function ($footer) {

            // 去掉`重置`按钮
            $footer->disableReset();


            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();



        });
        // 抛出成功信息
        $form->saved(function ($form) {

            $success = new MessageBag([
                'title'   => '保存成功',
                'message' => '保存成功',
            ]);

            return back()->with(compact('success'));
        });
        return $form;
    }
}
